﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using DevExpress.Web.ASPxTabControl;
using olierFrm.DataBase.FrameWork.StdQuery;
using System.Data;
using olierFrm.WebFormDev.Session;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.WebControls;

namespace olierFrm.WebFormDev.StdQuery
{
    public class DetailRowTemplate : ITemplate
    {
        #region ITemplate 成员

        public void InstantiateIn(System.Web.UI.Control container)
        {
            ASPxPageControl pageControl = new ASPxPageControl();
            SubQuery subQuery = new SubQuery(MasterStdQuery.StdQuery.SubQuery);
            var dtSource = (DataTable)MasterGrid.DataSource;
            int expandRowIndex = MasterGrid.GetExpandRow();
            if (SGrid.GetGroup(MasterGrid.GetQueryName()) != null && SGrid.GetGroup(MasterGrid.GetQueryName()).Count != 0)
            {
                expandRowIndex = expandRowIndex / SGrid.GetGroup(MasterGrid.GetQueryName()).Count - 1;
            }
            var curExpandRow = dtSource.Rows[expandRowIndex];

            string urlParam = "MasterQueryName=" + MasterGrid.GetQueryName();

            foreach (DataColumn dc in dtSource.Columns)
            {
                string value = "";

                if (dc.DataType.Name.IndexOf("DateTime") != -1 && curExpandRow[dc].ToString() != "")
                {
                    value = DateTime.Parse(curExpandRow[dc].ToString()).ToString("yyyy-MM-dd");
                }
                else
                {
                    value = curExpandRow[dc].ToString();
                }
                urlParam += "&" + dc.ColumnName + "=" + value;
            }
            //var first = true;
            foreach (var t in subQuery.SciptObject)
            {
                StdQuerySQL dbStdQuery = new StdQuerySQL(t.Value.HintQueryName);
                var tpGrid = new TabPage(t.Key);
                //是否第一个Tab，为了实现点击TAB时才加载
                //if (first)
                //{
                //    //tpGrid.Controls.Add(new LiteralControl("<iframe src='www.baidu.com'>assaas</iframe>"));
                //    tpGrid.Controls.Add(new LiteralControl("<iframe class='iFrameDetailGrid' style='width:100%; height:283px;' frameborder='no' framespacing='0' border='0' src='DetailGridPage.aspx?QueryName=" + t.Value.HintQueryName + "&" + urlParam + "' ></iframe>"));
                //    //tpGrid.Controls.Add(new LiteralControl("<script type='text/javascript'>alert(1);</script>"));
                //    first = false;
                //}
                //else
                tpGrid.Controls.Add(new LiteralControl("<iframe class='iFrameDetailGrid' style='width:100%; height:283px;' frameborder='no' framespacing='0' border='0' url='DetailGridPage.aspx?QueryName=" + t.Value.HintQueryName + "&" + urlParam + "' src='' ></iframe>"));
                pageControl.TabPages.Add(tpGrid);
                //UIFrm.BS.StdQuery.Chart chart = new StdQuery.Chart(dbStdQuery);
                if (dbStdQuery.StdQuery.InCharts != null && dbStdQuery.StdQuery.InCharts.Trim().Length != 0)
                {
                    InChart inChart = new InChart(dbStdQuery.StdQuery.InCharts);
                    int index = 0;
                    foreach (var c in inChart.SciptObject)
                    {
                        var tpChart = new TabPage(c.Key);
                        tpChart.Controls.Add(new LiteralControl("<iframe class='iFrameDetailChart' style='width:100%; height:370px;' frameborder='no' framespacing='0' border='0' url='ChartPage.aspx?QueryName=" + t.Value.HintQueryName + "&" + urlParam + "&_Chart=" + index++ + "' src=''></iframe>"));

                        pageControl.TabPages.Add(tpChart);
                    }
                }

            }
            //第一个把url属性赋给src，点击TAB时才加载
            pageControl.ClientSideEvents.TabClick = "function(s,e){if (e.tab.index!=0){" +
                        "var iframe=$(s.mainElement).find(\"tbody > tr:eq(1) > td > div:eq(\"+e.tab.index+\") > iframe\");" +
                        "if (iframe.attr(\"url\")){iframe.attr(\"src\",iframe.attr(\"url\"));iframe.removeAttr(\"url\");}" +
                        "}}";
            pageControl.ClientSideEvents.Init = "function(s,e){var iframe=$(s.mainElement).find(\"tbody > tr:eq(1) > td > div:eq(0) > iframe\"); iframe.attr(\"src\",iframe.attr(\"url\")); iframe.removeAttr(\"url\");}";
            pageControl.ClientSideEvents.ActiveTabChanging = "function(s, e) { processOnServer = false;cancel = false;reloadContentOnCallback = false;}";
            pageControl.AutoPostBack = false;
            pageControl.Width = new Unit("100%");
            pageControl.ContentStyle.Paddings.Padding = 0;
            container.Controls.Add(pageControl);
            //container.Controls.Add(new LiteralControl("<script>alert(1);var iframe=$(s.mainElement).find(\"tbody > tr:eq(1) > td > div:eq(0) > iframe\");alert(iframe.length); iframe.attr(\"src\",iframe.attr(\"url\")); iframe.removeAttr(\"url\");</script>"));

        }
        public StdQuerySQL MasterStdQuery { private get; set; }
        public ASPxGridView MasterGrid { private get; set; }
        #endregion
    }
}
